package kuaidi.view.activity;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import kuaidi.api.constanst.ExceptionHandle;
import kuaidi.api.sqlite.RecordsDao;
import kuaidi.entity.kuaidijieguo.Kdjg;
import kuaidi.entity.kuaidijieguo.ListBean;
import kuaidi.helper.R;
import kuaidi.presenter.KdjgPersenter;
import kuaidi.view.adapter.KdjgAdapter;
import kuaidi.view.impl.KdjgView;


public class SaomaActivity extends AppCompatActivity {

    @BindView(R.id.back_saoma)
    ImageButton backSaoma;
    @BindView(R.id.gengduo_saoma)
    ImageButton gengduoSaoma;
    @BindView(R.id.rlv_saoma)
    RecyclerView rlvSaoma;
    private String com;
    private String no;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_saoma);
        ButterKnife.bind(this);
        ArrayList<String> stringList = getIntent().getStringArrayListExtra("info");
        no=stringList.get(0);
        com=stringList.get(1);
        initView();
        initData();
    }

    private void initData() {
        KdjgPersenter p=new KdjgPersenter(new KdjgView() {
            @Override
            public void onSuccess(Kdjg kdjg) {
                fileSave(kdjg,no);
                data.addAll(kdjg.getResult().getList());
                kdjgAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFail(ExceptionHandle.ResponeThrowable e) {
                int i=0;
            }

            @Override
            public void onFinish() {

            }
        });
        p.getKdjgData(getParams());
    }

    private void fileSave(Kdjg kdjg,String name) {
        //保存在sd卡
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

            File sdCardDir = Environment.getExternalStorageDirectory();//获取SDCard目录
            File sdFile = new File(sdCardDir, name);
            try {
                FileOutputStream fos = new FileOutputStream(sdFile);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(kdjg);// 写入
                fos.close(); // 关闭输出流
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            saveno(name);
            Toast.makeText(this, "成功保存到sd卡", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "失败保存到sd卡", Toast.LENGTH_LONG).show();
        }
    }

    private void saveno(String name) {
        RecordsDao rd=new RecordsDao(this);
        rd.addRecords(name);
    }

    private List<ListBean> data;
    private GridLayoutManager manager;
    private KdjgAdapter kdjgAdapter;

    private void initView() {
        data=new ArrayList<>();
        manager= new GridLayoutManager(this,1);
        kdjgAdapter=new KdjgAdapter(this,data);
        rlvSaoma.setLayoutManager(manager);
        rlvSaoma.setAdapter(kdjgAdapter);
    }

    @OnClick({R.id.back_saoma, R.id.gengduo_saoma})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_saoma:
                finish();
                break;
            case R.id.gengduo_saoma:
                break;
        }
    }

    public Map<String,String> getParams() {
        HashMap<String, String> map = new HashMap<>();
        map.put("com", com);
        map.put("no", no);
        map.put("key", "7b077524b87980eea5f52e1d3127dd7c");
        map.put("dtype", "json");
        return map;
    }
}
